//
//  hostTableViewController.m
//  iNews
//
//  Created by lanou3g on 14-12-10.
//  Copyright (c) 2014年 张在青. All rights reserved.
//

#import "hostTableViewController.h"
#import "videoTableViewCell.h"
#import "movieHandel.h"
#import "movieDataModel.h"
//#import "UITableView+DragLoad.h"
#import "UIImage+ImageDown.h"
#import "videoDetailViewController.h"
#import "videorootViewController.h"

#import "MJRefresh.h"

@interface hostTableViewController ()

@end

@implementation hostTableViewController

- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    

    //注册cell
    
    [self.tableView registerClass:[videoTableViewCell class] forCellReuseIdentifier:@"cell_id"];
    
    //请求数据
 
    [movieHandel movieshareData].sdr = 0;
    
    [[movieHandel movieshareData] requestMovieDataWithString:kmoviewdataString key:@"V9LG4B3A0"];
    
    //添加kvo
    [[movieHandel movieshareData] addObserver:self forKeyPath:@"hostArray" options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld  context:nil];
    
    
    [self.tableView addHeaderWithTarget:self action:@selector(dragTableDidTriggerRefresh)];
    
    [self.tableView addFooterWithTarget:self action:@selector(dragTableDidTriggerLoadMore)];
    
    
    

}

#pragma mark 响应观察者
-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context{
    
    
    if ([keyPath isEqualToString:@"hostArray"]) {

        
        [self.tableView reloadData];
    }
    
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{

   
    return [[movieHandel movieshareData].hostArray count];
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
 videoTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell_id" forIndexPath:indexPath];
    
   
    movieDataModel *mda = [movieHandel movieshareData].hostArray[indexPath.row];
    
    cell.movieView.image = [UIImage imageWithUrlString:mda.cover myBlock:^(UIImage *image) {
        cell.movieView.image = image;
        
    }];
    
    //字符串替代
    NSString *sr = [mda.title stringByReplacingOccurrencesOfString:@"&quot;" withString:@"\""];
    cell.titlabel.text = sr;
    
 
   
    
    
    return cell;
}


-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

    return 180;

}

//点击cell跳入详情
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

    //给详情接口传入vid
    

        videoDetailViewController *videotail = [[[videoDetailViewController alloc] init] autorelease];
        
        movieDataModel *m = [movieHandel movieshareData].hostArray[indexPath.row];
        [movieHandel movieshareData].str = m.vid;
        
        videotail.str_url = m.mp4_url;
        videotail.videoTitle = m.title;
        videotail.appendString = @"recommend";
        
        
        //模态跳入详情页面
        //videotail.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
        
        //响应者链条
        videorootViewController *vrc = (videorootViewController *)self.nextResponder.nextResponder.nextResponder;
    
 
          [vrc presentViewController:videotail animated:YES completion:nil];
   
    
        
 
    
}      


//刷新的代理方法

#pragma mark 请求数据
- (void)dragTableDidTriggerRefresh{
    
   
    //请求数据
     [[movieHandel movieshareData] requestMovieDataWithString:kmoviewdataString key:@"V9LG4B3A0"];
  
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        
        
         [self.tableView  headerEndRefreshing];
         [self.tableView reloadData];
    });
    

    
}

//加载的代理方法
- (void)dragTableDidTriggerLoadMore{
    
  
    [movieHandel movieshareData].sdr += 10;
    
    [[movieHandel movieshareData] requestMovieDataWithString:kmoviewdataString key:@"V9LG4B3A0"];

    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        
      
        [self.tableView  footerEndRefreshing];
        [self.tableView reloadData];
    });
    

    
    
}



/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath
{
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
